package com.lwj.liwanjia.view.activity.seckill;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.SeckillMoreAdapter;
import com.lwj.liwanjia.adapter.SeckillTimeAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.SeckillMoreBean;
import com.lwj.liwanjia.bean.SeckillTimeBean;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.impl.SeckillMoreView;
import com.lwj.liwanjia.impl.SeckillTimeView;
import com.lwj.liwanjia.presenter.SeckillMorePresenter;
import com.lwj.liwanjia.presenter.SeckillTimePresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.utils.TimeCountDown;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SeckillMoreActivity extends BaseActivity implements SeckillMoreView, SeckillTimeView, SuperRecyclerView.LoadingListener {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.time_rlv)
    RecyclerView timeRlv;
    @BindView(R.id.hour)
    TextView hour;
    @BindView(R.id.minute)
    TextView minute;
    @BindView(R.id.second)
    TextView second;
    @BindView(R.id.sekill_rlv)
    SuperRecyclerView seckillRlv;
    @BindView(R.id.progress)
    ProgressBar progressBar;

    private SeckillTimeAdapter timeAdapter;
    private List<SeckillTimeBean.DataBean.TimeSlotBean> list = new ArrayList<>();
    private SeckillMoreAdapter seckillMoreAdapter;
    private List<SeckillMoreBean.DataBean.CommendityBean> seckillList = new ArrayList<>();
    private SeckillMorePresenter seckillMorePresenter;
    private SeckillTimePresenter seckillTimePresenter;
    private int page = 0;
    private int allPage;
    private String strTime;
    private TimeCountDown timeCountDown;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("秒杀商品");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        timeRlv.setLayoutManager(manager);
        timeAdapter = new SeckillTimeAdapter(this, list);
        timeRlv.setAdapter(timeAdapter);
        LinearLayoutManager manager1 = new LinearLayoutManager(this);
        manager1.setOrientation(LinearLayoutManager.VERTICAL);
        seckillRlv.setLayoutManager(manager1);
        seckillRlv.setLoadMoreEnabled(true);
        seckillRlv.setRefreshEnabled(true);
        seckillRlv.setLoadingListener(this);
        seckillMoreAdapter = new SeckillMoreAdapter(this, seckillList);
        seckillRlv.setAdapter(seckillMoreAdapter);
        seckillMorePresenter = new SeckillMorePresenter(this);
        seckillTimePresenter = new SeckillTimePresenter(this);
        listener();
    }

    private void listener() {
        seckillMoreAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(SeckillMoreActivity.this, SeckillDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("shopGoodsCode", seckillList.get(position).getCommodityCode());
                bundle.putString("commodityType", seckillList.get(position).getCommodityType());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        timeAdapter.setListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                page = 0;
                seckillRlv.setNoMore(false);
                strTime = list.get(position).getSeckillStartTime();
                timeAdapter.setClickPosition(position);
                timeAdapter.notifyDataSetChanged();
                seckillMorePresenter.getSeckillMore(page, list.get(position).getSeckillStartTime());
            }
        });
    }

    @Override
    protected void initData() {
        seckillTimePresenter.getSeckillTime();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_seckill_more;
    }


    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getSeckillTime(SeckillTimeBean seckillTimeBean) {
        if (seckillTimeBean.getState() == 1) {
            list.clear();
            list.addAll(seckillTimeBean.getData().getTimeSlot());
            timeAdapter.setNow(seckillTimeBean.getData().getNow());
            strTime = seckillTimeBean.getData().getNow();
            int position = seckillTimeBean.getData().getId();
            timeRlv.scrollToPosition(position-1);
            timeAdapter.notifyDataSetChanged();
            seckillMorePresenter.getSeckillMore(page, seckillTimeBean.getData().getNow());
        }
    }

    @Override
    public void getSeckillMore(SeckillMoreBean seckillMoreBean) {
      if (seckillMoreBean.getState() == 1) {
            allPage = seckillMoreBean.getData().get(0).getAllPage();
            long endTime = seckillMoreBean.getData().get(0).getEndTime();
            if (timeCountDown != null) {
                timeCountDown.cancel();
            }
            cutTime(endTime);
            if (page == 0) {
                seckillList.clear();
            }
            seckillList.addAll(seckillMoreBean.getData().get(0).getCommendity());
            seckillMoreAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onRefresh() {
        page = 0;
        seckillMorePresenter.getSeckillMore(page, strTime);
        seckillRlv.completeRefresh();
    }

    @Override
    public void onLoadMore() {
        if (page >= allPage - 1) {
            seckillRlv.setNoMore(true);
            return;
        }
        page++;
        seckillMorePresenter.getSeckillMore(page, strTime);
        seckillRlv.completeLoadMore();
    }

    //倒计时
    private void cutTime(long endTime) {
        long now = Calendar.getInstance().getTimeInMillis();
        long distance = endTime - now;
        timeCountDown = new TimeCountDown(distance, 1000, hour, minute, second);
        timeCountDown.start();
    }
}
